// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.viewers.script;

import java.util.Date;
import java.util.List;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.DenguesCorePlugin;
import org.dengues.core.module.IModuleScriptSynchronizer;
import org.dengues.core.resource.WarehouseResourceFactory;
import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.model.component.IMPORTType;
import org.dengues.model.warehouse.ScriptsStorage;
import org.dengues.model.warehouse.WarehouseFactory;
import org.dengues.warehouse.i18n.Messages;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.wizard.Wizard;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-7 qiang.zhang $
 * 
 */
public class NewJavaScriptsWizard extends Wizard {

    private NewJavaScriptsPage page;

    private IPath path;

    private ScriptsStorage storage;

    private boolean isActived;

    /**
     * Qiang.Zhang.Adolf@gmail.com NewJavaScriptsWizard constructor comment.
     * 
     * @param path
     */
    public NewJavaScriptsWizard(IPath path) {
        this();
        this.path = path;
        setWindowTitle(Messages.getString("NewJavaScriptsWizard.ctitle")); //$NON-NLS-1$
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com NewJavaScriptsWizard constructor comment.
     */
    private NewJavaScriptsWizard() {
        super();
        setDefaultPageImageDescriptor(ImageUtil.getDescriptor(IDenguesSharedImage.WAREHOUSE_WIZARD_PROCESS));
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com NewJavaScriptsWizard constructor comment.
     * 
     * @param storage
     * @param path
     * @param isActived
     */
    public NewJavaScriptsWizard(ScriptsStorage storage, IPath path, boolean isActived) {
        this();
        this.storage = storage;
        this.path = path;
        this.isActived = isActived;
        setWindowTitle(Messages.getString("NewJavaScriptsWizard.mtitle")); //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.wizard.Wizard#addPages()
     */
    @Override
    public void addPages() {
        if (storage != null) {
            page = new NewJavaScriptsPage(storage, isActived);
        } else {
            page = new NewJavaScriptsPage(path);
        }
        addPage(page);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.wizard.Wizard#performFinish()
     */
    @Override
    public boolean performFinish() {
        boolean create = false;
        boolean rename = false;
        String processName = page.getStorageName();
        String oldName = null;
        if (storage == null) {
            storage = WarehouseFactory.eINSTANCE.createScriptsStorage();
            storage.setUuid(WarehouseResourceFactory.getEMFId());
            storage.setCreation(new Date());
            create = true;
        } else if (path != null) {
            if (!storage.getName().equals(processName)) {
                rename = true;
                oldName = storage.getName();
                // WarehouseResourceFactory.deleteObjectDataPhysical(storage);
            }
        }
        List<IMPORTType> imports = page.getImports();
        for (IMPORTType type : imports) {
            String installCustomerModule = DenguesCorePlugin.installCustomerModule(type);
            type.setMODULE(installCustomerModule);
            storage.getImports().add(type);
        }
        storage.setComment(page.getStorageComment());
        storage.setName(processName);
        storage.setStatus(page.getStorageStatus());
        storage.setPath(path.toPortableString());
        storage.setModified(new Date());
        storage.setCategoryName(ENodeCategoryName.SCRIPTS.getName());
        IModuleScriptSynchronizer scriptSynchronizer = DenguesCorePlugin.getDefault().getDesignerCoreService()
                .getScriptSynchronizer();
        if (create) {
            String synchronizeScript = scriptSynchronizer.synchronizeScript(storage, true, oldName);
            storage.setContent(synchronizeScript);
        } else if (rename) {
            scriptSynchronizer.synchronizeScript(storage, false, oldName);
        }
        if (create) {
            WarehouseResourceFactory.createStorage(storage, path);
        } else {
            WarehouseResourceFactory.saveStorage(storage);
        }
        return true;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getJavaScript".
     * 
     * @return
     */
    public ScriptsStorage getJavaScript() {
        return storage;
    }

}
